package com.zy.imagineCommunity.service;

import com.zy.imagineCommunity.dao.UserMapper;
import com.zy.imagineCommunity.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserSerivce {

    @Autowired
    private UserMapper userMapper;

    /**
     * 通过email查询用户
     *
     * @param email
     * @return
     */
    public User queryUserByEmail(String email) {
        return userMapper.queryUserByEmail(email);
    }

    /**
     * 通过username查询用户
     *
     * @param username
     * @return
     */
    public User queryUserByUsername(String username) {
        return userMapper.queryUserByUsername(username);
    }

    /**
     * 通过username查询用户
     *
     * @param username
     * @return
     */
    public User queryUserByUsernameAndPassword(String username, String password) {
        return userMapper.queryUserByUsernameAndPassword(username, password);
    }

    public User queryUserByUserId(int userId) {
        return userMapper.queryUserByUserId(userId);
    }

    /**
     * 增加用户
     *
     * @param user
     */
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    /**
     * 修改用户
     *
     * @param user
     */
    public void updateUserByUserId(User user) {
        userMapper.updateUserByUserId(user);
    }

    /**
     * 根据页码、每页个数查询用户
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<UserText> queryUserTextByPageNoAndPageSize(int pageNo, int pageSize) {
        Page<UserText> page = new Page<UserText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = userMapper.queryUserTotalCount();
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<UserText> userTextList = userMapper.queryUserTextByBeginAndPageSize(begin, pageSize);
        //设置每页内容
        page.setItems(userTextList);
        return page;
    }

    //根据id删除用户id
    public void deleteUserById(int id) {
        userMapper.deleteUserById(id);
    }

    /**
     * 根据管理员类型查询用户的不同状态
     *
     * @param type
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<UserTextTwo> queryUserTextTwoByTypeAndPageNoAndPageSize(int type, int pageNo, int pageSize) {
        Page<UserTextTwo> page = new Page<UserTextTwo>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = userMapper.queryUserTotalCount();
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;

        List<UserText> userList = userMapper.queryUserTextByBeginAndPageSize(begin, pageSize);

        List<UserTextTwo> userTextTwoList = new ArrayList<UserTextTwo>();

        for (UserText userText : userList) {

            UserTextTwo userTextTwo = new UserTextTwo();

            userTextTwo.setId(userText.getId());

            userTextTwo.setUsername(userText.getUsername());

            userTextTwo.setPassword(userText.getPassword());

            userTextTwo.setEmail(userText.getEmail());

            userTextTwo.setHeadImg(userText.getHeadImg());

            userTextTwo.setCreateTime(userText.getCreateTime());

            userTextTwo.setValue(userText.getValue());

            if ("0".equals(userText.getLanguageState())) {

                userTextTwo.setState(0);

            } else {
                String stateString = userText.getLanguageState();

                String[] stateNumber = stateString.split("_");

                for (int i = 0; i < stateNumber.length; i++) {

                    int state = Integer.parseInt(stateNumber[i]);

                    if (state == 0) {
                        userTextTwo.setState(0);
                    }

                    if (state == type) {
                        userTextTwo.setState(type);
                    }

                    if (state == 1) {
                        userTextTwo.setState(1);
                    }

                }
            }

            userTextTwoList.add(userTextTwo);
        }
        //设置每页内容
        page.setItems(userTextTwoList);

        return page;
    }

    /**
     * 查询总用户数
     *
     * @return
     */
    public int queryUserTotalCount() {
        return userMapper.queryUserTotalCount();
    }

    /**
     * 根据姓名、页码、每页个数查询用户
     *
     * @param pageNo
     * @param pageSize
     * @param username
     * @return
     */
    public Page<UserText> queryUserTextByPageNoAndPageSizeAndUserName(int pageNo, int pageSize, String username, String userType) {
        Page<UserText> page = new Page<UserText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = userMapper.queryUserTotalCountByUserName(username, userType);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<UserText> userTextList = userMapper.queryUserTextByBeginAndPageSizeAndUserName(begin, pageSize, username, userType);
        //设置每页内容
        page.setItems(userTextList);
        return page;
    }

    /**
     * 查找所有用户
     *
     * @return
     */
    public List<User> queryUserList() {
        return userMapper.queryUserList();
    }

    ;

}
